//初始化数据库1
const db = wx.cloud.database({
  env: 'nzxyb-5gsp2tia41c3b11d'
})
Page({
  data: {
    orderUserName: '', //下单人姓名
    orderUserNumber: '', //下单人手机号
    orderQujianPlace: '', //快递取件地址
    orderShoujianPlace: '', //快递收件地址
    orderQujianCode: '', //快递取件码
    isAllowTake:false,//是否允许代取
  },

  //方法集****************************************************************************************

  //返回access_token
  /*   getAccessToken() {
      return new Promise((resolve, reject) => {
        wx.request({
          url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx78df91b6c76e22da&secret=74420f4f06c1cd98d9399a028cdf8f31',
          method: 'GET',
          success: (res) => {
            console.log('获取access_token成功', res)
            //返回access_token
            resolve(res.data.access_token)
          },
          fail(res) {
            console.log('获取access_token失败', res)
            reject({
              msg: '获取access_token失败',
              res: res
            })
          }
        })
      })
    }, //getAccessToken ending */

  /*   //选择取件码图片
    chooseQujianCodeImg() {
      wx.chooseImage({
        count: 1,
        success: (res) => { //选择取件码照片成功
          this.setData({ //更新imgUrl显示即将上传的图片
            orderUserIdImg: res.tempFilePaths[0]
          })
          console.log("选择照片成功", res)
        },
        fail: (res) => { //选择照片失败
          console.log("选择照片失败", res)
        }
      })
    }, */

  //下单接口
  uploadOrder() {
    //获取数据
    let orderUserName = this.data.orderUserName;
    let orderUserNumber = this.data.orderUserNumber;
    let orderQujianPlace = this.data.orderQujianPlace;
    let orderShoujianPlace = this.data.orderShoujianPlace;
    let orderQujianCode = this.data.orderQujianCode;
    if (wx.getStorageSync('iscustomer')) { //允许下单（判断用户权限）
      if (orderQujianCode && orderShoujianPlace && orderQujianPlace && orderUserNumber && orderUserName) { //允许提交订单（判断订单信息输入完整性）
        if(!this.data.isAllowTake){wx.showToast({
          title: '未同意代取',
          icon:"error"
        });return}
        //上传订单信息到云服务器
        db.collection('t_order').add({
          data: {
            orderUserInfo: { //下单用户信息
              openid: wx.getStorageSync('openid'),
              orderUserName: this.data.orderUserName,
              orderUserNumber: this.data.orderUserNumber,
              orderHeadImg: wx.getStorageSync('userHeadImg')
            },
            orderQujianPlace: this.data.orderQujianPlace, //取件地址
            orderShoujianPlace: this.data.orderShoujianPlace, //收件地址
            orderQujianCode: this.data.orderQujianCode, //取件码
            orderId: new Date().getFullYear() + "" + new Date().getMonth() + "" + new Date().getDate() + "" + new Date().getTime(), //订单号
            orderTime: new Date().toLocaleString(), //下单时间
            isOrder: 0, //订单状态（0：未接单； 1：已接单； 2：订单完成）
            isAllowTake:this.data.isAllowTake//订单是否允许代取
          },
          success: (res) => { //提交订单成功
            console.log("上传订单信息成功", res)
            wx.showToast({
              title: '下单成功',
              mask: true,
            })
          },
          fail: (res) => { //提交订单失败
            console.log("上传订单信心失败", res)
            wx.showToast({
              title: '提交订单失败',
              icon: "error",
              mask: true
            })
          }
        })
        // //上传身份码图片到云存储
        // wx.cloud.uploadFile({
        //   cloudPath: wx.getStorageSync("openid") + ".png", //云存储文件名
        //   filePath: this.data.orderUserIdImg, //上传的文件url
        //   success: (res) => { //上传取件码照片成功

        //   },
        //   fali: (res) => { //上传取件码照片失败
        //     console.log("上传照片失败", res)
        //   }
        // })
      } else { //不允许提交订单（信息不完整）
        wx.showToast({
          title: '请完善下单信息',
          icon: 'error',
          mask: true
        })
      }
    } else { //禁止下单（判断用户权限）
      wx.showToast({
        title: '抱歉，当前无权限下单，联系管理员',
        mask: true,
        icon: 'error',
        duration: 2000,

      })
    }
  },

  //是否同意代取单选框事件
  getIsAllowTake(e){
    console.log(e)
    this.setData({
      isAllowTake:e.detail
    })
  },








  //生命周期函数******************************************************
  onShow() {
    if (!wx.getStorageSync('userState')) { //拦截未登录用户
      wx.showToast({ //弹窗提示
        title: '请先登录',
        icon: 'error',
        mask: true,
        duration: 4000,
        complete() {
          wx.switchTab({ //跳转登录界面
            url: '/pages/mine/mine',
          })
        }
      })
    }
  },

  //分享
  onShareAppMessage(){
    return{
      title:"农职快递代拿",
      path:"/pages/index/index"
    }
  }
})